package com.my.wangpan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.vdisk.android.VDiskAuthSession;
import com.vdisk.android.VDiskDialogListener;
import com.vdisk.net.exception.VDiskDialogError;
import com.vdisk.net.exception.VDiskException;
import com.vdisk.net.session.AccessToken;
import com.vdisk.net.session.AppKeyPair;
import com.vdisk.net.session.Session;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class LoginActivity extends Activity implements VDiskDialogListener {

    /**== 一下是老师申请的，没有basic权限，不能直接访问目录，为了继续往下学，所以使用demo里面的几个东西=*/
//    public static final String CONSUMER_KEY = "3237555059";
//    public static final String CONSUMER_SECRET = "2b6c964b071e2ecc28c1835628cc6901";
//    public static final String REDIRECT_URL = "http://billy.itheima.com";
    /***
     * 以下的是 api demo里面的东西
     */
    public static final String CONSUMER_KEY = "2330724462";
    public static final String CONSUMER_SECRET = "04f81fc56cc936bfc8f0fa1cef285158";
    public static final String REDIRECT_URL = "http://vauth.appsina.com/callback1.php";
    Button mBtnLogin;
    private VDiskAuthSession session;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        initView();
        initData();
        initListener();
//        initActionBar();

    }

//    private void initActionBar() {
//
//        ActionBar actionBar = getSupportActionBar();
//        actionBar.setTitle("黑马网盘");
//        actionBar.setDisplayShowHomeEnabled(true);
//    }

    private void init() {
        /**
         * 初始化 Init
         */
        AppKeyPair appKeyPair = new AppKeyPair(CONSUMER_KEY, CONSUMER_SECRET);
        /**
         * @AccessType.APP_FOLDER - sandbox 模式
         * @AccessType.VDISK - basic 模式
         */
        session = VDiskAuthSession.getInstance(this, appKeyPair, Session.AccessType.VDISK);
        //若果session没有过期，不需要跳到主界面
        if(session.isLinked()){
            startActivity(new Intent(this,MainActivity.class));
            finish();
        }
    }

    private void initData() {

    }

    private void initView() {
        mBtnLogin = (Button) findViewById(R.id.btnlogin);
    }

    private void initListener() {
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //1,发起授权请求---》让用户输入密码/账户


                //2,处理授权的结果--》得到accessToken
                // 使用微盘Token认证，需设置重定向网址
                // Need to set REDIRECT_URL if you want to use VDisk token.
                session.setRedirectUrl(REDIRECT_URL);
                session.authorize(LoginActivity.this, LoginActivity.this);

            }
        });
    }

    /**
     * 处理
     * @param values
     */

    @Override
    public void onComplete(Bundle values) {//授权结束
        AccessToken mToken = null;
        if (values != null) {
            mToken = (AccessToken) values.getSerializable(VDiskAuthSession.OAUTH2_TOKEN);
            session.finishAuthorize(mToken);//把touken绑定到session
            Toast.makeText(getApplicationContext(),mToken.getToken(),Toast.LENGTH_SHORT).show();
        }
        File file = new File("/data/data/sdcard/aaasss.txt");
        try {
            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(mToken.getToken().getBytes());
            outputStream.flush();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void onError(VDiskDialogError error) {//授权错误

    }

    @Override
    public void onVDiskException(VDiskException exception) {

    }

    @Override
    public void onCancel() {//授权取消

    }
}
